package hs4m.kidseducational.math;

import hs4m.kidseducational.R;
import hs4m.kidseducational.memory.MemoryActivity;
import hs4m.kidseducational.util.Utils;

import java.util.Collections;
import java.util.Random;
import java.util.Vector;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MathActivity extends Activity {

	public Typeface typeFace;
	EditText txtQuestion;
	Button btnAns_1;
	Button btnAns_2;
	Button btnAns_3;
	Button btnAns_4;
	int mRightButton = 0;
	int mRightCnt = 0;
	int mWrongCnt = 0;
	TextView right;
	TextView wrong;
	TextView result;
	int numQuestion = 10;
	Vector<Integer> vector = new Vector<Integer>();
	MediaPlayer mMedia;
	String mOper;
	int mLevel;

	// public static final void setAppFont(ViewGroup mContainer, Typeface mFont)
	// {
	// if (mContainer == null || mFont == null)
	// return;
	//
	// final int mCount = mContainer.getChildCount();
	//
	// // Loop through all of the children.
	// for (int i = 0; i < mCount; ++i) {
	// final View mChild = mContainer.getChildAt(i);
	// if (mChild instanceof TextView) {
	// // Set the font if it is a TextView.
	// ((TextView) mChild).setTypeface(mFont);
	// } else if (mChild instanceof ViewGroup) {
	// // Recursively attempt another ViewGroup.
	// setAppFont((ViewGroup) mChild, mFont);
	// }
	// }
	// }

	/** Play the sound using android.media.MediaPlayer */
	public void playSound(int soundID) {
		if (mMedia != null) {
			mMedia.reset();
		}
		mMedia = MediaPlayer.create(getApplicationContext(), soundID);
		mMedia.start();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_math);
		// typeFace = Typeface.createFromAsset(getAssets(), "fonts/VNFREE.TTF");
		// typeFace = Typeface.createFromAsset(getAssets(),
		// "fonts/danielbk.ttf");
		typeFace = Typeface.createFromAsset(getAssets(), "fonts/daniel.ttf");
		// typeFace = Typeface.createFromAsset(getAssets(),
		// "fonts/desyrel.ttf");

		final ViewGroup mContainer = (ViewGroup) findViewById(
				android.R.id.content).getRootView();
		Utils.setAppFont(mContainer, typeFace);

		txtQuestion = (EditText) findViewById(R.id.question);
		btnAns_1 = (Button) findViewById(R.id.button1);
		btnAns_2 = (Button) findViewById(R.id.button2);
		btnAns_3 = (Button) findViewById(R.id.button3);
		btnAns_4 = (Button) findViewById(R.id.button4);

		result = (TextView) findViewById(R.id.lblResult);
		right = (TextView) findViewById(R.id.lblRightCount);
		wrong = (TextView) findViewById(R.id.lblWrongCount);

		// MinhVT - Cai dat cap do choi
		mOper = (String) getIntent().getStringExtra("operator");
		mLevel = (int) getIntent().getIntExtra("level", 1);

		nextQuestion();
	}

	private void nextQuestion() {
		numQuestion--;
		if (numQuestion >= 0) {
			// Clear Vector
			vector.clear();
			MathFuntion mathFuntion = new MathFuntion(getApplicationContext());
			mathFuntion.genQuestion(mLevel, mOper);
			txtQuestion.setText(mathFuntion.mQuestion);

			Random ran = new Random();
			mRightButton = ran.nextInt(3);

			if ("+".equals(mOper)) {
				vector.add(mathFuntion.mNumA + mathFuntion.mNumB);
				// Lay ket qua ngau nhien
				if (0 == mRightButton) {
					btnAns_1.setText(String.valueOf(mathFuntion.mNumA
							+ mathFuntion.mNumB));
				} else {
					btnAns_1.setText(String.valueOf(getNumberRandom()));
				}
				if (1 == mRightButton) {
					btnAns_2.setText(String.valueOf(mathFuntion.mNumA
							+ mathFuntion.mNumB));
				} else {
					btnAns_2.setText(String.valueOf(getNumberRandom()));
				}

				if (2 == mRightButton) {
					btnAns_3.setText(String.valueOf(mathFuntion.mNumA
							+ mathFuntion.mNumB));
				} else {
					btnAns_3.setText(String.valueOf(getNumberRandom()));
				}
				if (3 == mRightButton) {
					btnAns_4.setText(String.valueOf(mathFuntion.mNumA
							+ mathFuntion.mNumB));
				} else {
					btnAns_4.setText(String.valueOf(getNumberRandom()));
				}
			} else if ("-".equals(mOper)) {
				vector.add(mathFuntion.mNumA - mathFuntion.mNumB);
				// Lay ket qua ngau nhien
				if (0 == mRightButton) {
					btnAns_1.setText(String.valueOf(mathFuntion.mNumA
							- mathFuntion.mNumB));
				} else {
					btnAns_1.setText(String.valueOf(getNumberRandom()));
				}
				if (1 == mRightButton) {
					btnAns_2.setText(String.valueOf(mathFuntion.mNumA
							- mathFuntion.mNumB));
				} else {
					btnAns_2.setText(String.valueOf(getNumberRandom()));
				}

				if (2 == mRightButton) {
					btnAns_3.setText(String.valueOf(mathFuntion.mNumA
							- mathFuntion.mNumB));
				} else {
					btnAns_3.setText(String.valueOf(getNumberRandom()));
				}
				if (3 == mRightButton) {
					btnAns_4.setText(String.valueOf(mathFuntion.mNumA
							- mathFuntion.mNumB));
				} else {
					btnAns_4.setText(String.valueOf(getNumberRandom()));
				}
			} else if ("*".equals(mOper)) {
				vector.add(mathFuntion.mNumA * mathFuntion.mNumB);
				// Lay ket qua ngau nhien
				if (0 == mRightButton) {
					btnAns_1.setText(String.valueOf(mathFuntion.mNumA
							* mathFuntion.mNumB));
				} else {
					btnAns_1.setText(String.valueOf(getNumberRandom()));
				}
				if (1 == mRightButton) {
					btnAns_2.setText(String.valueOf(mathFuntion.mNumA
							* mathFuntion.mNumB));
				} else {
					btnAns_2.setText(String.valueOf(getNumberRandom()));
				}

				if (2 == mRightButton) {
					btnAns_3.setText(String.valueOf(mathFuntion.mNumA
							* mathFuntion.mNumB));
				} else {
					btnAns_3.setText(String.valueOf(getNumberRandom()));
				}
				if (3 == mRightButton) {
					btnAns_4.setText(String.valueOf(mathFuntion.mNumA
							* mathFuntion.mNumB));
				} else {
					btnAns_4.setText(String.valueOf(getNumberRandom()));
				}
			} else if ("/".equals(mOper)) {
				vector.add(mathFuntion.mNumA / mathFuntion.mNumB);
				// Lay ket qua ngau nhien
				if (0 == mRightButton) {
					btnAns_1.setText(String.valueOf(mathFuntion.mNumA
							/ mathFuntion.mNumB));
				} else {
					btnAns_1.setText(String.valueOf(getNumberRandom()));
				}
				if (1 == mRightButton) {
					btnAns_2.setText(String.valueOf(mathFuntion.mNumA
							/ mathFuntion.mNumB));
				} else {
					btnAns_2.setText(String.valueOf(getNumberRandom()));
				}

				if (2 == mRightButton) {
					btnAns_3.setText(String.valueOf(mathFuntion.mNumA
							/ mathFuntion.mNumB));
				} else {
					btnAns_3.setText(String.valueOf(getNumberRandom()));
				}
				if (3 == mRightButton) {
					btnAns_4.setText(String.valueOf(mathFuntion.mNumA
							/ mathFuntion.mNumB));
				} else {
					btnAns_4.setText(String.valueOf(getNumberRandom()));
				}
			} else if (">".equals(mOper)) {
				vector.add(mathFuntion.mNumA);
				btnAns_1.setText(String.valueOf(mathFuntion.mNumA));
				btnAns_2.setText(String.valueOf(getNumberRandom()));
				btnAns_3.setText(String.valueOf(getNumberRandom()));
				btnAns_4.setText(String.valueOf(getNumberRandom()));				
				
				mRightButton = getRightButtonPos(true);
			} else if ("<".equals(mOper)) {
				vector.add(mathFuntion.mNumA);
				btnAns_1.setText(String.valueOf(mathFuntion.mNumA));
				btnAns_2.setText(String.valueOf(getNumberRandom()));
				btnAns_3.setText(String.valueOf(getNumberRandom()));
				btnAns_4.setText(String.valueOf(getNumberRandom()));		
				
				mRightButton = getRightButtonPos(false);
			}
		} else {
			result.setText(getResources().getString(R.string.lblTotalPoint)
					+ ": " + mRightCnt + "/" + "10");
			btnAns_1.setEnabled(false);
			btnAns_2.setEnabled(false);
			btnAns_3.setEnabled(false);
			btnAns_4.setEnabled(false);
			openDialog();
		}
	}

	private void showResult(boolean isRight) {
		if (isRight) {
			mRightCnt++;
			result.setText(getResources().getString(R.string.lblTrue));
			right.setText(getResources().getString(R.string.lblTrue) + ": "
					+ mRightCnt);
		} else {
			mWrongCnt++;
			result.setText(getResources().getString(R.string.lblWrong));
			wrong.setText(getResources().getString(R.string.lblWrong) + ": "
					+ mWrongCnt);
		}
	}

	public void checkRight1(View view) {
		if (0 == mRightButton) {
			showResult(true);
			nextQuestion();
		} else {
			showResult(false);
			nextQuestion();
		}
	}

	public void checkRight2(View view) {
		if (1 == mRightButton) {
			showResult(true);
			nextQuestion();
		} else {
			showResult(false);
			nextQuestion();
		}
	}

	public void checkRight3(View view) {
		if (2 == mRightButton) {
			showResult(true);
			nextQuestion();
		} else {
			showResult(false);
			nextQuestion();
		}
	}

	public void checkRight4(View view) {
		if (3 == mRightButton) {
			showResult(true);
			nextQuestion();
		} else {
			showResult(false);
			nextQuestion();
		}
	}

	private int getNumberRandom() {
		MathFuntion mathFuntion = new MathFuntion(getApplicationContext());
		while (true) {
			int result = mathFuntion.genNum(mLevel);
			if (!vector.contains(result)) {
				vector.add(result);
				return result;
			}
		}
	}

	private int getRightButtonPos(boolean isGreatest) {
		int num;
		if (isGreatest) {
			num = Collections.max(vector);
			// Log.e("So LON Nhat", "# " + num);
		} else {
			num = Collections.min(vector);
			// Log.e("So nho Nhat", "# " + num);
		}
		
		int pos = 0;
		for (int i = 0; i < vector.size(); i++) {
			if (num == vector.get(i)) {
				pos = i;
				break;
			}
		}
		// Log.e("VI TRI", "# " + pos);
		return pos;
	}

	private int getNumberRandom(boolean isGreatest, int numA) {
		MathFuntion mathFuntion = new MathFuntion(getApplicationContext());
		int cnt = 1000;
		while (true) {
			cnt--;
			if (cnt < 0) {
				return 888888;
			}
			int result = mathFuntion.genNum(mLevel);
			if (!vector.contains(result)) {
				if (isGreatest) {
					if (result < numA) {
						vector.add(result);
						return result;
					}
				} else {
					if (result > numA) {
						vector.add(result);
						return result;
					}
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_math, menu);

		return true;
	}

	public void resetOnclick(View view) {
		Intent it = getIntent();
		finish();
		startActivity(it);
	}

	private void openDialog() {
		final Dialog dialog = new Dialog(MathActivity.this,
				android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

		// dialog.setTitle("Kids Education");
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_layout);
		dialog.setCancelable(false);
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

		TextView result = (TextView) dialog.getWindow().findViewById(
				R.id.mark_result);
		result.setText(getResources().getString(R.string.lblTotalPoint) + ": "
				+ mRightCnt + "/" + "10");

		ImageButton btnMenu = (ImageButton) dialog.getWindow().findViewById(
				R.id.btnMenu);
		btnMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Go to Menu
				finish();
			}
		});
		ImageButton btnDismiss = (ImageButton) dialog.getWindow().findViewById(
				R.id.dismiss);
		btnDismiss.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				// Refresh
				finish();
				startActivity(getIntent());
			}
		});

		// An Sao di
		ImageView star1 = (ImageView) dialog.getWindow().findViewById(
				R.id.Star1);
		ImageView star2 = (ImageView) dialog.getWindow().findViewById(
				R.id.Star2);
		ImageView star3 = (ImageView) dialog.getWindow().findViewById(
				R.id.Star3);

		if (mRightCnt <= 3) {
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.GONE);
			star3.setVisibility(View.GONE);
			playSound(R.raw.disapointed);
		} else if (mRightCnt > 3 && mRightCnt <= 7) {
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			star3.setVisibility(View.GONE);
			playSound(R.raw.disapointed);
		} else if (mRightCnt > 7) {
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			star3.setVisibility(View.VISIBLE);
			playSound(R.raw.handclap_1);
		}

		dialog.show();
	}
}
